package com.bw.router

import android.content.Context
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Interceptor
import com.alibaba.android.arouter.facade.callback.InterceptorCallback
import com.alibaba.android.arouter.facade.template.IInterceptor
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.constants.ApiConst
import com.bw.base.constants.RoutePath
import com.tencent.mmkv.MMKV

/**
 * Desc: Token路由拦截器
 */

@Interceptor(priority = 5, name = "路由Token拦截器")
class RouteTokenInterceptor : IInterceptor {

    private lateinit var mContext: Context

    override fun init(context: Context?) {
        if (context != null) {
            mContext = context
        }
    }

    override fun process(postcard: Postcard?, callback: InterceptorCallback?) {
        val token = MMKV.defaultMMKV().decodeString(ApiConst.PARAM_TOKEN, "")
        if (token.isNullOrEmpty() &&
            RoutePath.PATH_LOGIN != postcard?.path &&
            RoutePath.PATH_REGISTER != postcard?.path
        ) {
            postcard?.group = RoutePath.GROUP_LOGIN
            postcard?.path = RoutePath.PATH_LOGIN
            postcard?.navigation()
        } else {
            callback?.onContinue(postcard)
        }
    }
}